﻿using AttributeRouting.Web.Http;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using WebTech.Coursework02.Models;
using WebTech.Coursework02.Utilities;

namespace WebTech.Coursework02.Controllers
{
    public class EmployeeController : Controller
    {
        private ExtractionEntities db = new ExtractionEntities();

  

        //
        // GET: /Employee/

        public ActionResult Index()
        {
            return View(db.employees.ToList());
        }


        //
        // GET: /Employee/Details/5
        [AllowAnonymous]
        public ActionResult Details(int id)
        {
            employee employee = db.employees.Find(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            return View(employee);
        }

        [Authorize (Roles= "HRManager")]
        public ActionResult SalaryHistory()
        {
            int empNo;
            int.TryParse(User.Identity.Name, out empNo);
            employee employee = db.employees.FirstOrDefault(x => x.emp_no == empNo);
            return View(employee.salaries);
        }

        [AllowAnonymous]  
        public IQueryable<employee> PopulateEmployees()
        {         
            IEnumerable<employee> employees = db.employees.ToList().Select(x => new employee() {emp_no = x.emp_no, first_name = x.first_name, last_name  = x.last_name , birth_date = x.birth_date});
            var foo = employees.AsQueryable();
            return foo;
        }


        //public ActionResult Details(string firstName, string lastName)
        //{ 
        
        //}

        //
        // GET: /Employee/Create

       
        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(GridEmployee grid)
        {
            employee employee = new employee() {
                emp_no = 499999,
                first_name = grid.first_name,
                last_name = grid.last_name,
                gender = grid.gender,
                hire_date = grid.hire_date,
                birth_date = grid.birth_date
            
            };

            db.employees.Add(employee);
            db.SaveChanges();
            return Json("success");
        }

        //
        // GET: /Employee/Edit/5

        public ActionResult Edit(int id)
        {
            employee employee = db.employees.Find(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            return View(employee);
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Entry(employee).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(employee);
        }

        //
        // GET: /Employee/Delete/5

        public ActionResult Delete(int id = 0)
        {
            employee employee = db.employees.Find(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            return View(employee);
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            employee employee = db.employees.Find(id);
            db.employees.Remove(employee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //[Authorize(Roles = "HRManager")]
        [POST("employee/GetSalaryLog")]
        public JsonResult GetSalaryLog(Request request)
        {
            System.Func<salary, GridSalary> secondarySelector = x => new GridSalary() { empNo = x.emp_no, salary = x.salary1, startDate = x.from_date, endDate = x.to_date};            
            int empNo;
            // TODO Remove the user of USER.IDENTITY
            int.TryParse(request.emp_no, out empNo);
            employee employee = db.employees.FirstOrDefault(x => x.emp_no == empNo);
            IEnumerable<GridSalary> salaries = employee.salaries.Select(secondarySelector);
            IQueryable<GridSalary> salaryQuery = salaries.Skip(request.skip).Take(request.take).AsQueryable();

            return this.Json(new Response(salaryQuery.ToArray(), salaries.Count()), JsonRequestBehavior.AllowGet);
        }

        [POST("employee/GetTitle")]
        public JsonResult GetTitles(Request request)
        {
            System.Func<title, GridTitle> secondarySelector = x => new GridTitle() { empNo = x.emp_no, title = x.title1, fromDate = x.from_date, toDate = x.to_date };
            int empNo;
            // TODO Remove the user of USER.IDENTITY
            int.TryParse(request.emp_no, out empNo);
            employee employee = db.employees.FirstOrDefault(x => x.emp_no == empNo);
            IEnumerable<GridTitle> titles = employee.titles.Select(secondarySelector);
            IQueryable<GridTitle> titleQuery = titles.Skip(request.skip).Take(request.take).AsQueryable();

            return this.Json(new Response(titleQuery.ToArray(), titles.Count()), JsonRequestBehavior.AllowGet);
        }

        [POST("employee/GetLatestId")]
        public JsonResult GetLatestId()
        {
            //var lastEmp = db.employees.OrderBy(x => x.emp_no).ToList();
            int maxEmployeeId = 1; //lastEmp.Last().emp_no + 1;
            return this.Json(string.Format("empNo: {0}", maxEmployeeId));
        }        

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}